package cn.bugstack.springframework.test;

import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
import cn.bugstack.springframework.test.bean.User;
import cn.bugstack.springframework.test.bean.UserDao;
import cn.bugstack.springframework.test.bean.UserService;
import cn.hutool.system.UserInfo;
import jdk.nashorn.internal.runtime.options.Option;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Optional;
import java.util.OptionalInt;

/**
 * 博客：https://bugstack.cn - 沉淀、分享、成长，让自己和他人都能有所收获！
 * 公众号：bugstack虫洞栈
 * Create by 小傅哥(fustack)
 */
public class ApiTest2 {

    @Test
    public void test_BeanFactory() {
        // 1.初始化 BeanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //2. 手动注入userDao
        beanFactory.registerBeanDefinition("userDao", new BeanDefinition(UserDao.class));


        PropertyValue name = new PropertyValue("userName", "张三");
        PropertyValue age = new PropertyValue("age", "14");
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addPropertyValue(name);
        propertyValues.addPropertyValue(age);
        propertyValues.addPropertyValue(new PropertyValue("userDao", new BeanReference("userDao")));
        // 2.新建一个bean的定义这里写的非常简单，就是传了一个clazz
        BeanDefinition beanDefinition = new BeanDefinition(UserService.class, propertyValues);

        // 3.将bean定义注册到工厂,这样在获取不到bean的时候就会通过定义生成新的bean,如果是单例模式，还会缓存起来
        beanFactory.registerBeanDefinition("userService", beanDefinition);
        // 4.获取bean
        UserService userService = null;
        try {
            userService = (UserService) beanFactory.getBean("userService");
            userService.queryUserInfo();
            Integer userAge = userService.getAge();
            System.out.println(userAge);

            UserDao userDao = userService.getUserDao();
            String s = userDao.queryUserName("10001");
            System.out.println(s);
        } catch (BeansException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {


//        UserService userService = new UserService();
//
//        Class<? extends UserService> aClass = userService.getClass();
//        try {
//            Field age = aClass.getDeclaredField("age");
//            age.setAccessible(true);
//            age.set(userService,12);
//            System.out.println(userService.getAge());
//        } catch (NoSuchFieldException | IllegalAccessException e) {
//            e.printStackTrace();
//        }


//          互联网医院

        getStr(3);
        User user = new User();
        user.setUserName("黄哥");
        user.setAge("12");
        user.setGender("1");


    }

    private static Optional<String> getErrorMsg(String msg) {

        return Optional.ofNullable(msg);


    }

    private static String getStr(String str) {
        return "AA" + str;
    }


    private static void getStr(int i) {


        if (i == 1) {

        } else if (i > 1) {
            if (i == 2) {

            } else if (i == 3) {
                System.out.println("3");
                return;
            }

        }
        System.out.println("1");
    }

}
